package com.dailywork.basepro.thread.message;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * wait notify���� , wait�ͷ���,notify���ͷ��� ʹ��ͬһ�������
 *
 * @author Administrator
 */
public class ListAdd2 {

    private static volatile List list = new ArrayList();

    public void add() {
        list.add("bjsxt");
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final ListAdd2 list2 = new ListAdd2();

        // 1.ʵ��������һ��lock
        // ��ʹ��wait��notify��ʱ��,һ��Ҫ�����synchronized�ؼ���ȥʹ��
        final Object lock = new Object(); // �Ѷ��󵱳�һ����
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        Thread t1 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                try {
                                    synchronized (lock) {
                                        for (int i = 0; i < 10; i++) {
                                            list2.add();
                                            System.out.println(
                                                    "��ǰ�߳�: " + Thread.currentThread().getName() + "�����һ��Ԫ��..");
                                            Thread.sleep(500);
                                            if (list2.size() == 5) {
                                                System.out.println("�Ѿ�����֪ͨ!");
                                                // countDownLatch.countDown();        //����֪ͨ,������,ֻ��һ����־
                                                lock.notify();
                                            }
                                        }
                                    }
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        },
                        "t1");

        Thread t2 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                synchronized (lock) { // ��������
                                    if (list2.size() == 5) {
                                        try {
                                            System.out.println("t2����... ");
                                            //					countDownLatch.await();
                                            lock.wait(); // �յ�notif֪ͨ����������
                                        } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                    System.out.println(
                                            "��ǰ�߳��յ�֪ͨ: " + Thread.currentThread().getName() + "�յ�֪ͨ �߳�ֹͣ...");
                                    throw new RuntimeException();
                                }
                            }
                        },
                        "t2");

        t2.start();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t1.start();
    }
}
